import json


from xhsapi import XhsApi


def main():

    xhsapi = XhsApi('e1fbcf46b2a66597')

    xhsapi.init_new_device_params()

    register_smid_ret = xhsapi.register_smid()
    print('register_smid_ret:' + register_smid_ret)

    smid = json.loads(register_smid_ret)['detail']['deviceId']
    print('smid:' + smid)
    xhsapi.set_smid(smid)

    session_id = ''
    active_user_result = xhsapi.active_user()
    print('active_user_result:' + active_user_result)
    active_user_result_data = json.loads(active_user_result)['data']
    if 'session' in active_user_result_data:
        session_id = active_user_result_data['session']
        print('session_id:' + session_id)

    exit()

    # wb_username = "wnbyil1568@163.com"
    # wb_pwd = "5pl11xksr3"

    # # 微博app登录
    # if len(session_id) == 0:
    #     session_id = xhsapi.login_with_weibo_app(wb_username, wb_pwd)
    #     print('session_id:' + session_id)
    #
    # # 微博h5登录
    # if len(session_id) == 0:
    #     session_id = xhsapi.login_with_weibo_h5(wb_username, wb_pwd)
    #     print('session_id:' + session_id)

    # # 短信验证码登录
    if len(session_id) == 0:
    #     # 发送短信验证码
        phone = input(">>>请输入手机号")
        send_code_ret = xhsapi.send_sms_code(phone, '1')
        print('send_code_ret:' + send_code_ret)

        sms_code = input('请输入短信验证码：')
        print('sms_code:' + sms_code)

        # 验证短信验证码
        check_code_ret = xhsapi.check_sms_code(phone, sms_code, '1')
        print('check_code_ret:' + check_code_ret)

        # 用短信验证码登录
        mobile_token = json.loads(check_code_ret)['data']['token']
        print('mobile_token:' + mobile_token)
        session_id = xhsapi.login_with_sms(phone, mobile_token)
        print('session_id:' + session_id)

    if len(session_id) == 0:
        print('session is empty, exit!')
        exit()

    # 设置session_id
    xhsapi.set_session_id(session_id)

    # 获取登录用户信息
    login_user_info = xhsapi.get_login_userinfo()
    print('login_user_info:' + login_user_info)

    # # 刷新session, 隔2-3小时刷新一次
    # new_session = xhsapi.refresh_session()
    # print('new_session:' + new_session)
    # xhsapi.set_session_id(new_session)

    # # 获取首页推荐
    # home_feed = xhsapi.get_home_feed()
    # print('home_feed:' + home_feed)

    # 获取用户信息
    user_info = xhsapi.get_user_info('5c3fe348000000000603daaf')
    print('user_info:' + user_info)

    print(xhsapi.get_register_user_data())

    # # 获取用户笔记
    # user_note = xhsapi.get_user_note('5c3fe348000000000603daaf', 1, 10)
    # print('user_note:' + user_note)
    #
    # # 获取用户视频
    # user_videos = xhsapi.get_user_videos('5c3fe348000000000603daaf', 1, 10)
    # print('user_videos:' + user_videos)
    #
    # # 获取用户关注列表
    # user_followings = xhsapi.get_user_followings('5c3fe348000000000603daaf')
    # print('user_followings:' + user_followings)
    #
    # # 获取视频笔记详情
    # video_note_detail = xhsapi.get_video_detail('5e24319d00000000010070ba', '5e2432a500000000010004e8')
    # print('video_note_detail:' + video_note_detail)
    #
    # # 获取笔记详情
    # note_detail = xhsapi.get_note_detail('5e21f851000000000100b7d2')
    # print('note_detail:' + note_detail)
    #
    # # 获取用户粉丝
    # user_followers = xhsapi.get_user_followers('5c3fe348000000000603daaf')
    # print('user_followers:' + user_followers)
    #
    # # 搜索
    # search_ret = xhsapi.search('护肤', 1, 20)
    # print('search_ret:' + search_ret)
    #
    # # 获取评论
    # note_id = '5cdd5813000000000f00dd77'
    # comment_list = xhsapi.get_note_comments(note_id, 10)
    # print("comment_list:" + comment_list)
    #
    # # 获取话题相关的日记
    # topic_notes = xhsapi.get_topic_notes('5cc6ce90ec9d135e33e97b7e', 1, 14)
    # print('topic_notes:' + topic_notes)

    # 获取接口使用情况
    api_access_info = xhsapi.get_api_access_info()
    print('api_access_info:' + api_access_info)

    return

    # # 手机号密码登录
    # phone = '19919373386'
    # passwd = 'w123456'
    # passwd_login_ret = xhsapi.login_with_passwd(phone, passwd)
    # print('passwd_login_ret:' + passwd_login_ret)


if __name__ == '__main__':
    main()